Service Methods Operation/Flows
1. shorten(originalUrl: string): Promise of { shortUrl: string }
Purpose: Generate a shortened version of the given original URL. If the URL already exists in the database, return the existing shortened version.
Flow:
-
Start
-
Search for an existing ShortUrl entity by
originalUrl- If found → format and return existing short URL
-
Else:
- Generate a new short code using
nanoid(8) - Create a new ShortUrl entity with
originalUrlandshortCode - Save the new entity
- Format and return the new short URL
- Generate a new short code using
2. resolve(shortCode: string): Promise of string or null
Purpose: Resolve a short code back to the original full URL.
Flow:
-
Start
-
Look up ShortUrl entity by
shortCode- If found → return
originalUrl - Else → return
null
- If found → return
3. formatShortUrl(domain: string, shortCode: string): { shortUrl: string }
Purpose: Generate a properly formatted short URL using the domain and short code.
Flow:
- Trim trailing slashes from
domain - Append
/su/followed by theshortCode - Return the full
shortUrlas an object
Note: This is a private utility method used internally in the service.